<?php

class Default_CartController extends Zend_Controller_Action
{

    public function init()
    {
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('add', 'html')
                    ->addActionContext('process', 'json')
                    ->addActionContext('dropMenu', 'html')
                    ->addActionContext('remove', 'json')
                    ->addActionContext('countItem','json')
                    ->initContext();
    }

    public function indexAction()
    {
        $tmp = $this->cals();
        $this->view->subtotal = $tmp['total'];
        $this->view->cart = $tmp['product'];
        $page = new Zend_Navigation_Page_Mvc(array(
                'module'=>'default',
                'action'     => 'cart',
                'controller' => 'index'
        ));
        
        
    }

    public function cals()
    {
        $session = new Zend_Session_Namespace('cart');
        if(isset($session->cart) and !empty($session->cart)){
        	$ids = array_keys($session->cart);
        	$counts = array_values($session->cart);
        	
        	$product = new Default_Model_Product();
        	$tmp = $product->id($ids)->toArray();
        	$subtotal = 0;
        	$i = 0;
        	foreach ($session->cart as $id => $num)
        	{
        		$tmp[$i]['so_luong_mua'] = $num;
        		$subtotal += $tmp[$i]['gia_ban'] * $tmp[$i]['so_luong_mua'];
        		$i++;
        	}
        	$session->total = $subtotal;
        	return array('total' => $subtotal , 'product'=> $tmp ,'size' => array_sum($counts));
        }else
        {
          $session->total  = 0;
          return array('total' => 0 , 'product'=> array() ,'size' => 0);     	
        }
    }

    public function processAction()
    {
        $arr = $this->cals();
        echo json_encode(array('total'=>$arr['total'],'num'=> $arr['size']));
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout()->disableLayout();
    }

    public function addAction()
    {
        $id = $this->_request->getParam('id');
        $num = $this->_request->getParam('num',1);

        $session = new Zend_Session_Namespace('cart');
        $cart = isset($session->cart[$id]) ? $session->cart[$id] : 0;
        
        $product = new Default_Model_Product();
        
        if($product->addCart($id,$num))
        { 
            $cart += $num;
        }
        
        $session->cart[$id] = $cart;
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
    }

    public function removeAction()
    {
        $id = $this->_request->getParam('id');
        $session = new Zend_Session_Namespace('cart');
        
        $product = new Default_Model_Product();
        $product->removeCart($id,$session->cart[$id]);
        unset($session->cart[$id]);
        $arr = $this->cals();
        echo json_encode(array('total'=>$arr['total'],'num'=> $arr['size']));
        
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout()->disableLayout();
    }

    public function clearAllAction()
    {
        $session = new Zend_Session_Namespace('cart');
        $product = new Default_Model_Product();
        if(isset($session->cart))
        {
            foreach ($session->cart as $key =>$value)
            {
            	$product->removeCart($key,$value);
            	unset($session->cart[$key]);
            }
        }
        
    }

    public function dropMenuAction()
    {
        $arr = $this->cals();
        $this->view->total =isset($arr['total']) ? $arr['total'] : 0;
        $this->view->cart = isset($arr['product']) ? $arr['product'] : array() ;
        $this->_helper->layout->disableLayout();
    }

    public function checkoutAction()
    {
        // action body
    }

    public function countItemAction()
    {
        $id = $this->_request->getParam('id');
        $num = $this->_request->getParam('num');
        
        /*Kiểm tra xem id có trong giỏ hàng ?*/
        $session = new Zend_Session_Namespace("cart");
        if(!array_key_exists($id,$session->cart))return;
        
        $product = new Default_Model_Product();
        $session->cart[$id] = $num;
        $product->resize($id, $num);
        $arr = $this->cals();
        echo json_encode(array('total'=>$arr['total'],'num'=> $arr['size']));
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout()->disableLayout();
    }


}

















